package com.w3coffee.web.control;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.w3coffee.web.Branch;
import com.w3coffee.web.User;
import com.w3coffee.web.dao.CoffeeDao;
import com.w3coffee.web.dao.model.TCheckin;
import com.w3coffee.web.dao.model.TUser;

public class CheckinToken {
	/** 持久化对象 */
	private TCheckin po;

	private User user;
	private Branch branch;
	private Long checkinTime;

	/** GPS定位经度 */
	private Double longitude;
	/** GPS定位纬度 */
	private Double latitude;
	/** GPS定位精确度（单位：米） */
	private Double accuracy;

	public CheckinToken() {
		this.po = new TCheckin();
		this.user = new User();
	}

	public CheckinToken(int id) {
		this();

		CoffeeDao dao = (CoffeeDao) ContextServlet.getBean("coffeeDao");
		try {
			po = dao.getCheckinDetail(id);
			if (po == null) {
				return;
			}

			TUser tu = dao.getUser(po.getUid());
			if (tu != null) {
				user = new User(tu);
			}

			branch = ContextServlet.getBranch(po.getBid());
			if (branch != null) {
				checkinTime = po.getTime().getTime();
			}
		} catch (Exception e) {
			Logger logger = LoggerFactory.getLogger(getClass());
			logger.error("Construct CheckinToken by checkin-ID error.", e);
		}
	}

	public int getId() {
		return po == null ? -1 : po.getId();
	}

	public TCheckin encapsulate(HttpServletRequest req) {
		po.setUid(user.getUid());
		po.setBid(branch.getId());
		po.setRemoteIp(req.getRemoteAddr());
		checkinTime = new Date().getTime();
		po.setTime(new Date(checkinTime));
		po.setUserAgent(req.getHeader("user-agent"));
		return po;
	}

	public TCheckin getPo() {
		return po;
	}

	public CheckinAction getCheckinAction() {
		if (user == null) {
			return CheckinAction.login_and_verify_location;
		} else if (!user.isValid()) {
			return CheckinAction.login_and_verify_location;
		}

		if (branch == null) {
			return CheckinAction.only_verify_location;
		}

		if (checkinTime == null) {
			return CheckinAction.only_verify_location;
		} else {
			long interval = System.currentTimeMillis() - checkinTime;
			if (interval >= 0
					&& interval <= ContextServlet.getConfig()
							.getCheckinAvailablePeriod()) {
				return CheckinAction.already_checkin;
			} else {
				return CheckinAction.only_verify_location;
			}
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public Long getCheckinTime() {
		return checkinTime;
	}

	public void setCheckinTime(Long checkinTime) {
		this.checkinTime = checkinTime;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Double getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(Double accuracy) {
		this.accuracy = accuracy;
	}
}
